<template>
  <div>
    <sticky-top>
      <div class="title">
        <span>{{ isCreate ? '创建监控房间' : '更新监控房间' }}</span>
        <span class="back" @click="back"> <i class="iconfont icon-fanhui"></i> 返回 </span>
        <el-divider></el-divider>
      </div>
    </sticky-top>
    <div class="container">
      <div class="wrap">
        <el-row>
          <el-col :lg="16" :md="20" :sm="24" :xs="24">
            <el-form :model="form" status-icon ref="form" label-width="100px" @submit.native.prevent>
              <el-form-item label="房间状态" prop="manager">
                <el-input size="medium" v-model="form.mr_state" placeholder="请填写房间状态"></el-input>
              </el-form-item>
              <el-form-item label="房间名称" prop="name">
                <el-input size="medium" v-model="form.mr_name" placeholder="请填写房间名称"></el-input>
              </el-form-item>
<!--              <el-form-item label="联系方式" prop="phone">-->
<!--                <el-input size="medium" v-model="form.phone" placeholder="请填写联系方式"></el-input>-->
<!--              </el-form-item>-->

<!--              <el-form-item label="所在楼栋" prop="category_id">-->
<!--                <el-autocomplete-->
<!--                  @focus="loadBuildingSuggestions"-->
<!--                  popper-class="my-autocomplete"-->
<!--                  class="inline-input"-->
<!--                  v-model="form.belong_building"-->
<!--                  :fetch-suggestions="queryCategorySearch"-->
<!--                  placeholder="请选择所在楼栋"-->
<!--                  @select="handleCategorySelect"-->
<!--                >-->
<!--                  <template slot-scope="{ item }">-->
<!--                    <span class="id">{{ item.id }}</span>-->
<!--                    <span class="name">{{ item.name }}</span>-->
<!--                  </template>-->
<!--                </el-autocomplete>-->
<!--              </el-form-item>-->

<!--              <el-form-item label="所在楼层" prop="floor">-->
<!--                <el-input size="medium" v-model="form.floor" placeholder="请填写房间所在楼层"></el-input>-->
<!--              </el-form-item>-->


              <!--              <el-form-item label="描述" prop="description">-->
              <!--                <el-input size="medium" v-model="form.description" placeholder="请填写描述"></el-input>-->
              <!--              </el-form-item>-->

              <el-form-item class="submit">
                <el-button
                  v-permission="{ permission: ['创建监控房间', '更新监控房间'], type: 'disabled' }"
                  type="primary"
                  @click="submitForm('form')"
                >保 存</el-button
                >
                <el-button @click="resetForm('form')">重 置</el-button>
              </el-form-item>
            </el-form>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
import MonitoringRoom from '@/model/ag_monitoring_room'

export default {
  props: {
    isCreate: {
      type: Boolean,
      default: false,
    },
    roomId: {
      type: String,
      default: null,
    },
  },
  data() {
    return {
      form: {
        // building_id:'',
        // belong_building:'',
        mr_name: '',
        mr_state: '',
        // phone: '',
        // floor:'',
      },
      rules: {
        minWidth: 100,
        minHeight: 100,
        maxSize: 5,
      },
      // buildingName: '',
      // buildings: [],
      // specKeySuggestions: [],
      // skuSuggestions: [],
    }
  },
  async mounted() {
    if (!this.isCreate) {
      // const res = await MonitoringRoom.getMonitoringRoom(this.roomId)
      const res = await MonitoringRoom.getMonitoringRoom(this.roomId)
      this.form = res
      this.initData = [
        {
          id: res.id,
        },
      ]
      // this.buildingName=res.building_id
    }
  },
  methods: {
    // eslint-disable-next-line
    async submitForm(formName) {
      const postData = { ...this.form }
      let res
      if (this.isCreate) {
        res = await MonitoringRoom.addMonitoringRoom(postData)
      } else {
        res = await MonitoringRoom.editMonitoringRoom(this.roomId, postData)
      }
      if (res.code < window.MAX_SUCCESS_CODE) {
        this.$message.success(`${res.message}`)
        // this.resetForm(formName)
        this.$confirm('是否返回上一页', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(async () => {
          this.$emit('editClose')
        })
      }
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    back() {
      this.$emit('editClose')
    },
  },
}
</script>

<style lang="scss" scoped>
.el-divider--horizontal {
  margin: 0;
}

.container {
  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 16px;
    font-weight: 500;
    text-indent: 40px;

    .back {
      float: right;
      margin-right: 40px;
      cursor: pointer;
    }
  }

  .wrap {
    padding: 20px;
  }

  .submit {
    float: left;
  }
}
</style>
